package com.homework;

import java.util.concurrent.CyclicBarrier;

public class Method03 implements Runnable {
    private String result;
    private CyclicBarrier cyclicBarrier;

    public Method03( CyclicBarrier cyclicBarrier){
        this.cyclicBarrier = cyclicBarrier;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(1000);
            result = "123456";
            cyclicBarrier.await();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        long start = System.currentTimeMillis();

        System.out.println("Main Thread Start!");

        CyclicBarrier cyclicBarrier = new CyclicBarrier(2);

        Method03 method03 = new Method03(cyclicBarrier);
        Thread t = new Thread(method03);
        t.start();
        cyclicBarrier.await();

        // 可以改为通过redis存储获取
        System.out.println("异步处理结果为：" + method03.result);

        System.out.println("使用时间：" + (System.currentTimeMillis() - start) + " ms");
        System.out.println("Main Thread End!");
    }
}
